package com.app;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class NewsDetailActivity extends AppCompatActivity {

    private TextView tcNewsContent;
    private TextView tvNewsDate;
    private TextView tvNewsTitle;
    private ImageView ivBack;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);

        initView();
        initEvent();
        initData();

    }

    private void initData() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = "http://www.xhjjyst.com/freshmenapp/news/findone";
        JSONObject body = new JSONObject();
        try {
            body.put("id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Request request = new JsonObjectRequest(Request.Method.POST, url, body, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                jsonObject = jsonObject.optJSONObject("data");
                tvNewsDate.setText(jsonObject.optString("date"));
                tvNewsTitle.setText(jsonObject.optString("title"));
                tcNewsContent.setText(jsonObject.optString("content"));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(NewsDetailActivity.this, "失败"+volleyError, Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }

    private void initEvent() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NewsDetailActivity.this,NewsListActivity.class));
            }
        });
    }

    private void initView() {
        tvNewsTitle = findViewById(R.id.tv_news_title);
        tvNewsDate = findViewById(R.id.tv_news_date);
        tcNewsContent = findViewById(R.id.tv_news_content);
        ivBack = findViewById(R.id.iv_back);
    }

}